//
//  BookingDetailController.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/26/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "BookingDetailController.h"

@interface BookingDetailController ()

@end

@implementation BookingDetailController

@synthesize viewDetail, lbTitle, lbDate, lbPrice, scrollView, paidUser, lbCurrency;

@synthesize arrayPersons, symbol, booking;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    arrayPersons = [[NSMutableArray alloc] init];
    [self loadData];
    [self createNavigation];
    [self changeStyle];
    [self showGripUser];
}

-(void) loadData
{
    Helper *helper = [Helper instance];
    Event *event = [Event instance];
    User *user = [User instance];
    Entry *entry = [Entry instance];
    self.arrayPersons = [user getMemberByEventWS:event._id];
    symbol = [helper showSymbol:event.currency];
    lbCurrency.text = symbol;
    booking = [[BookingClass alloc] init];
    booking = [entry getDetailWS:entry._id];
    lbTitle.text = booking.bookingTitle;
    lbDate.text = booking.bookingDate;
    lbPrice.text = [StyleControl formatCurrency:[NSString stringWithFormat:@"%.2f",booking.bookingPrice] addCurency:YES];
    ParticipateClass *par = [[ParticipateClass alloc] init];
    par.firstMail = booking.email;
    par.image = booking.picture;
    par.idString = booking.idString;
    paidUser.image = [Helper getImageMember:par];
}

-(void)createNavigation
{
    UIButton *left =[UIButton buttonWithType:UIButtonTypeCustom];
    [left setImage:[UIImage imageNamed:@"buttonlist"] forState:UIControlStateNormal];
    left.frame = CGRectMake(8, 8, 37, 35);
    [left addTarget:self action:@selector(openListBooking) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *right =[UIButton buttonWithType:UIButtonTypeCustom];
    [right setTitle:@"Wijzig" forState:UIControlStateNormal];
    right.frame = CGRectMake(255, 8, 60, 35);
    
    [right addTarget:self action:@selector(editBooking) forControlEvents:UIControlEventTouchUpInside];
    Event *event = [Event instance];
    
    [StyleControl  naviStyle:self.view andTitle:event._name andColor:0 andleftButton:left andRightButton:right borderLeft:1 borderRight:1];
}
-(void) changeStyle
{
    [StyleControl background:self.view];
    [StyleControl setViewStyle:self.viewDetail];
    [StyleControl drawLine:self.viewDetail PositionX:0 PositionY:45 Width:310];
    [StyleControl drawLine:self.viewDetail PositionX:0 PositionY:90 Width:310];
    [StyleControl drawLine:self.viewDetail PositionX:0 PositionY:135 Width:310];
    [StyleControl setPriceStyle:lbPrice];
    [StyleControl setDateStyle:lbDate];
    [StyleControl setBorderStyle:paidUser];
}
-(void)showGripUser
{
    //[scrollView setBackgroundColor:[UIColor blueColor]];
    //scroll view
    for(UIView *subview in [self.scrollView subviews]) {
        [subview removeFromSuperview];
        //NSLog(@"Subviews Count=%d",myScrollView.subviews.count);
    }
    //number items per row
    int a=4;
    //number items per column
    int b=2;
    if([arrayPersons count] < 7) b = 2;
    //total items + 1 for add item in last
    int c=[self.arrayPersons count];
    //width of scrollView
    int d=scrollView.frame.size.width;
    //height of scrollView
    int d2=scrollView.frame.size.height;
    //width of an item
    int e=d/a;
    //height of an item
    int f=d2/b-15;
    int i=0;
    int numPages = ceil(1.0f*c/(a*b));
    for (int p=0; p<numPages; p++) {
        for (int m=0; m<b; m++) {
            for (int n=0; n<a && i<c; n++) {
                UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(((p*d)+(n*e)), (m*f), e, f)];
                                    
                    //NSLog(@"x: %d; y: %d ", (p*d)+(n*e), (m*f) );
                    ParticipateClass  *par = [self.arrayPersons objectAtIndex:i];
                    
                    UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
                    im.image = [UIImage imageNamed:@"member"];
                    im.image = [Helper getImageMember:par];
                    [StyleControl setBorderStyle:im];
                    [itemView addSubview:im];
                    [im release];
                
                    UILabel *number = [[UILabel alloc] init];
                    number.frame = CGRectMake(20, 65, 30, 15);
                
                    number.text = [NSString stringWithFormat:@"%d x",par.quantity];
                for(int m=0;m<[booking.participants count];m++){
                    ParticipateClass *p = [booking.participants objectAtIndex:m];
                    if(p.userId == par.userId){
                        number.text = [NSString stringWithFormat:@"%d x",p.quantity];
                    }
                }
                    [number setFont:[UIFont systemFontOfSize:10]];
                    [itemView addSubview:number];
                    [number release];
                
                    UIImageView *icon = [[UIImageView alloc] init];
                    icon.frame = CGRectMake(40, 65, 15, 15);
                    icon.image = [UIImage imageNamed:@"icon-user-01"];
                    [itemView addSubview:icon];
                    [icon release];
                    
                    UILabel *lblName = [[UILabel alloc] init];
                    lblName.text = par.name;
                    lblName.frame = CGRectMake(10, 65, 60, 20);
                    lblName.font = [UIFont systemFontOfSize:12];
                    //[itemView addSubview:lblName];
                    [lblName release];
                
                [scrollView addSubview:itemView];
                [itemView release];
                i++;
            }
        }
    }
    scrollView.contentSize = CGSizeMake(d*(numPages), d2);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
}

-(void)openListBooking
{
    BookingListController *page=[[BookingListController alloc]init];
    [self presentModalViewController:page animated:YES];
    [page release];
}
-(void) editBooking
{
    BookingEditController *page=[[BookingEditController alloc]init];
    [self presentModalViewController:page animated:YES];
    [page release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
